package com.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.entity.XueshengEntity;
import com.entity.XueshengxuankeEntity;
import com.entity.ZuoyebuzhiEntity;
import com.service.XueshengService;
import com.service.XueshengxuankeService;
import com.service.ZuoyebuzhiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.KechengxinxiEntity;
import com.service.KechengxinxiService;
import com.utils.PageUtils;
import com.utils.R;

import javax.servlet.http.HttpServletRequest;

/**
 * 课程查询
 */
@RestController
@RequestMapping("/xueshengxuanke2")
public class Xueshengxuanke2Controller {
    @Autowired
    private KechengxinxiService kechengxinxiService;

    @Autowired
    private XueshengxuankeService xueshengxuankeService;

    @Autowired
    private XueshengService xueshengService;

    @Autowired
    private ZuoyebuzhiService zuoyebuzhiService;

    /**
     * 查询课程列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request) {
        // 构建查询条件
        EntityWrapper<KechengxinxiEntity> ew = new EntityWrapper<KechengxinxiEntity>();
        
        // 课程名称模糊查询
        String kechengmingcheng = (String)params.get("kechengmingcheng");
        if(kechengmingcheng != null && !"".equals(kechengmingcheng)) {
            ew.like("kechengmingcheng", kechengmingcheng);
        }
        
        // 课程类型精确查询
        String kechengleixing = (String)params.get("kechengleixing");
        if(kechengleixing != null && !"".equals(kechengleixing)) {
            ew.eq("kechengleixing", kechengleixing);
        }
        
        // 学期精确查询
        String xueqi = (String)params.get("xueqi");
        if(xueqi != null && !"".equals(xueqi)) {
            ew.eq("xueqi", xueqi);
        }

        // 班级过滤
        String xuehao = (String) request.getSession().getAttribute("username");
        String banji = xueshengService.selectOne(new EntityWrapper<XueshengEntity>()
                .eq("xuehao", xuehao)).getBanji();
        ew.eq("banji", banji);

        // 分页查询
        PageUtils page = kechengxinxiService.queryPage(params, ew);

        return R.ok().put("data", page);
    }

    /**
     * 获取课程详情
     */
    @GetMapping("/detail")
    public R detail(@RequestParam Long id) {
        KechengxinxiEntity kechengxinxi = kechengxinxiService.selectById(id);
        return R.ok().put("data", kechengxinxi);
    }

    /**
     * 查询课程是否被选择
     */
    @GetMapping("/kecheng/{courseName}")
    public R get(@PathVariable String courseName, HttpServletRequest request) {
        String username = (String) request.getSession().getAttribute("username");
        int count = xueshengxuankeService.selectCount(new EntityWrapper<XueshengxuankeEntity>()
                .like("kechengmingcheng", courseName)
                .like("xuehao", username));
        return R.ok().put("data", count>0);
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody XueshengxuankeEntity xueshengxuanke, HttpServletRequest request){
        xueshengxuanke.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(xueshengxuanke);
        xueshengxuankeService.insert(xueshengxuanke);
        return R.ok();
    }

    /**
     * 选课操作
     */
    @PostMapping("/select")
    public R selectCourse(@RequestBody XueshengxuankeEntity xueshengxuanke, HttpServletRequest request) {
        // 获取当前登录学生信息
        String xuehao = (String) request.getSession().getAttribute("username");
        
        // 检查是否已选过该课程
        int count = xueshengxuankeService.selectCount(new EntityWrapper<XueshengxuankeEntity>()
                .eq("kechengmingcheng", xueshengxuanke.getKechengmingcheng())
                .eq("xuehao", xuehao));
                
        if (count > 0) {
            return R.error("您已经选过这门课程了");
        }
        
        // 设置选课信息
        xueshengxuanke.setXuehao(xuehao);
        xueshengxuanke.setXueshengxingming(xueshengService.selectOne(new EntityWrapper<XueshengEntity>()
                .like("xuehao", xuehao)).getXueshengxingming());
        xueshengxuanke.setXuankeriqi(new Date());
        
        // 保存选课记录
        xueshengxuankeService.insert(xueshengxuanke);
        
        return R.ok("选课成功");
    }

    /**
     * 退课操作
     */
    @PostMapping("/cancel")
    public R cancelCourse(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        String xuehao = (String) request.getSession().getAttribute("username");
        String kechengmingcheng = (String) params.get("kechengmingcheng");

        // 查找选课记录
        EntityWrapper<XueshengxuankeEntity> ew = new EntityWrapper<XueshengxuankeEntity>();
        ew.eq("xuehao", xuehao);
        ew.eq("kechengmingcheng", kechengmingcheng);

        // 检查学生是否选择了课程对应的课题，若有选择课题，不允许退课
        int count = zuoyebuzhiService.selectCount(new EntityWrapper<ZuoyebuzhiEntity>()
                .eq("xuehao", xuehao)
                .eq("kechengmingcheng", kechengmingcheng));
        if(count > 0) {
            return R.error("已选择该课程对应的课题，请取消课题后重试");
        }

        // 删除选课记录
        boolean result = xueshengxuankeService.delete(ew);
        
        if(result) {
            return R.ok("退课成功");
        } else {
            return R.error("退课失败，请重试");
        }
    }
}
